/**
 * 
 */
package com.ambow.servlet;

import java.io.IOException;

import java.util.List;

import javax.jws.WebService;
import javax.security.auth.message.callback.PrivateKeyCallback.Request;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ambow.Entity.Stock;
import com.ambow.service.StockService;
import com.ambow.service.impl.StockServiceImpl;

/**
 * @author Administrator
 *
 */
@WebServlet("/StockServlet")
public class StockServlet extends HttpServlet {
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
	 * javax.servlet.http.HttpServletResponse)
	 */

	StockService stockService = new StockServiceImpl();

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
	 * javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		String status = request.getParameter("status");// 根据状态进行相应的操作
		if (status.equals("query")) {
			String bookName = request.getParameter("bookName");
			String Shelf = request.getParameter("Shelf");
			System.out.println("bookName:" + bookName);
			System.out.println("Shelf:" + Shelf);
			if (bookName !="") {
				querym(bookName, request, response);// 根据教材名进行模糊查询
			} else if (Shelf != "") {
				queryShelf(Shelf, request, response);// 根据书架查询

			} else {
				querAll(request, response);
			}

		}
	}

	private void queryShelf(String shelf, HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String[] param = { shelf };
		List<Stock> list = stockService.Query("from Stock  as stock where stock.cell.name = ?", param);
		System.out.println("queryshelf:" + list.size());
		request.setAttribute("list", list);
		request.getRequestDispatcher("pages/module/storage/storage003/stockpile.jsp").forward(request, response);

	}

	private void querym(String str, HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String[] param = { "%" + str + "%" };
		List<Stock> list = stockService.Query("from Stock as stock where stock.textBook.name like ?", param);
		System.out.println("querym:" + list.size());
		request.setAttribute("list", list);
		request.getRequestDispatcher("pages/module/storage/storage003/stockpile.jsp").forward(request, response);

	}

	private void querAll(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub

		List<Stock> list = stockService.Query("from Stock", null);
		System.out.println("stock��zhi:" + list.size());
		request.setAttribute("list", list);
		request.getRequestDispatcher("pages/module/storage/storage003/stockpile.jsp").forward(request, response);

	}
}
